package com.yuandian.management.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.management.service.ConfWeekdaysService;
import com.yuandian.management.vo.Weekday;
import com.yuandian.management.vo.WeekdayParams;
import com.yuandian.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.text.ParseException;
import java.util.List;

/**
 * <p>
 * 工作日和节假日配置Controller
 * <p>
 *
 * @author lijunhang
 * @date 2021/12/13
 * @since 1.0.0
 */
@RestController
@RequestMapping("/weekdays")
@Api(tags = "工作日配置")
public class ConfWeekdaysController {

    @Autowired
    private ConfWeekdaysService service;

    /**
     * 新增某一年的工作日和节假日
     * @param params
     * @return
     * @throws ParseException
     */
    @PostMapping("addOneYearDays")
	@ApiOperation(value = "新增某一年的工作日和节假日", notes = "新增某一年的工作日和节假日")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "body", required = true, dataType = "String"),
	})
    public R addOneYearDays(@RequestBody @ApiIgnore WeekdayParams params) {
        if (service.getCountByTimeRange(params)>0){
            return R.failed(null,"当前时间范围存在工作日和节假日数据，不能重复插入");
        }
        if (service.addOneYearDays(params)){
            return R.ok("新增成功");
        }else{
            return R.failed("新增失败");
        }
    }

    /**
     * 根据具体的日期，修改工作日和节假日状态
     * @param params
     * @return
     */
    @PostMapping("updateTypeByDay")
	@ApiOperation(value = "修改工作日和节假日状态", notes = "修改工作日和节假日状态")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "day", value = "日期", paramType = "body", required = true, dataTypeClass = String.class),
			@ApiImplicitParam(name = "type", value = "类型 1:工作日 2：节假日", paramType = "body", required = true, dataTypeClass = Integer.class),
	})
    public R updateTypeByDay(@RequestBody @ApiIgnore WeekdayParams params){
        if (StringUtils.isBlank(params.getDay())||params.getType()==null){
            return R.failed(null,"参数错误");
        }
        if (service.updateTypeByDay(params)>=0){
            return R.ok("修改成功");
        }else{
            return R.failed("修改失败");
        }
    }


    /**
     * 根据开始时间和结束时间，删除一整年的节假日和工作日数据
     * @param params 请求参数
     * @return
     */
    @PostMapping("deleteByTimeRange")
	@ApiOperation(value = "删除一整年的节假日和工作日数据", notes = "删除一整年的节假日和工作日数据")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "body", required = true, dataType = "String"),
	})
    public R deleteByTimeRange(@RequestBody @ApiIgnore WeekdayParams params){
        if (StringUtils.isBlank(params.getStartTime()) || StringUtils.isBlank(params.getEndTime())){
            return R.failed(null,"参数错误");
        }
        if (service.queryPageWeekday(params).getTotal() == 0){
            return R.failed(null,"当前年份已不存在，不能重复删除");
        }
        if (service.deleteByTimeRange(params) >= 0){
            return R.ok("删除成功");
        }else{
            return R.failed("删除失败");
        }
    }


    /**
     * 工作日和节假日配置表分页查询
     * @param params 请求参数
     * @return
     */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "type", value = "类型 1:工作日 2：节假日", paramType = "body", required = true, dataTypeClass = Integer.class),
	})
	@ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping("/queryPageWeekday")
    public R queryPageWeekday(@RequestBody @ApiIgnore WeekdayParams params){
		Page<Weekday> pageDataInfo=service.queryPageWeekday(params);
        return R.ok(pageDataInfo);
    }

    /**
     * 工作日和节假日配置表不分页查询
     * @param params 请求参数
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping("/queryNoPageWeekday")
    public R< List<Weekday>> queryNoPageWeekday(@RequestBody WeekdayParams params){
        List<Weekday> pageDataInfo=service.queryNoPageWeekday(params);
        return R.ok(pageDataInfo);
    }
}
